package com.beemans.corekit.integration.admob;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;

public class AbAdvertLoadManager {
    private AbAdvertLoadManager() {
    }

    private static final class Holder {
        private static final AbAdvertLoadManager INSTANCE = new AbAdvertLoadManager();
    }

    public static AbAdvertLoadManager getInstance() {
        return Holder.INSTANCE;
    }


    // =================================== 信息流广告 ===================================

    private final List<AbNativeAdWrapper> nativeAdReadyList = new CopyOnWriteArrayList<>();
    private final List<AbNativeAdWrapper> nativeAdLoadingList = new CopyOnWriteArrayList<>();
    private final Object nativeAdLock = new Object();

    public boolean isNativeAdvertReady(@NonNull String placementId) {
        return nativeAdReadyList.stream()
                .anyMatch(adWrapper -> Objects.equals(adWrapper.getPlacementId(), placementId) && adWrapper.isAdvertReady());
    }

    public boolean isNativeAdvertLoading(@NonNull String placementId) {
        return nativeAdLoadingList.stream()
                .anyMatch(adWrapper -> Objects.equals(adWrapper.getPlacementId(), placementId) && adWrapper.isAdvertLoading());
    }

    public void updateNativeReadyList(@NonNull AbNativeAdWrapper adWrapper, boolean isRemove) {
        synchronized (nativeAdLock) {
            if (isRemove) {
                nativeAdReadyList.remove(adWrapper);
            } else if (!nativeAdReadyList.contains(adWrapper)) {
                nativeAdReadyList.add(adWrapper);
            }
        }
    }

    public void updateNativeLoadingList(@NonNull AbNativeAdWrapper adWrapper, boolean isRemove) {
        synchronized (nativeAdLock) {
            if (isRemove) {
                nativeAdLoadingList.remove(adWrapper);
            } else if (!nativeAdLoadingList.contains(adWrapper)) {
                nativeAdLoadingList.add(adWrapper);
            }
        }
    }

    @Nullable
    public AbNativeAdWrapper getReadyNativeAdWrapper(@NonNull String placementId) {
        return nativeAdReadyList.stream()
                .filter(adWrapper -> Objects.equals(adWrapper.getPlacementId(), placementId) && adWrapper.isAdvertReady())
                .findFirst()
                .orElse(null);
    }

    // =================================== 信息流广告 ===================================


    // =================================== 开屏广告 ===================================

    private final List<AbSplashAdWrapper> splashAdReadyList = new CopyOnWriteArrayList<>();
    private final List<AbSplashAdWrapper> splashAdLoadingList = new CopyOnWriteArrayList<>();
    private final Object splashAdLock = new Object();

    public boolean isSplashAdvertReady(@NonNull String placementId) {
        return splashAdReadyList.stream()
                .anyMatch(adWrapper -> Objects.equals(adWrapper.getPlacementId(), placementId) && adWrapper.isAdvertReady());
    }

    public boolean isSplashAdvertLoading(@NonNull String placementId) {
        return splashAdLoadingList.stream()
                .anyMatch(adWrapper -> Objects.equals(adWrapper.getPlacementId(), placementId) && adWrapper.isAdvertLoading());
    }

    @Nullable
    public AbSplashAdWrapper getReadySplashAdWrapper(@NonNull String placementId) {
        return splashAdReadyList.stream()
                .filter(adWrapper -> Objects.equals(adWrapper.getPlacementId(), placementId) && adWrapper.isAdvertReady())
                .findFirst()
                .orElse(null);
    }

    public void updateSplashReadyList(@NonNull AbSplashAdWrapper adWrapper, boolean isRemove) {
        synchronized (splashAdLock) {
            if (isRemove) {
                splashAdReadyList.remove(adWrapper);
            } else if (!splashAdReadyList.contains(adWrapper)) {
                splashAdReadyList.add(adWrapper);
            }
        }
    }

    public void updateSplashLoadingList(@NonNull AbSplashAdWrapper adWrapper, boolean isRemove) {
        synchronized (splashAdLock) {
            if (isRemove) {
                splashAdLoadingList.remove(adWrapper);
            } else if (!splashAdLoadingList.contains(adWrapper)) {
                splashAdLoadingList.add(adWrapper);
            }
        }
    }

    // =================================== 开屏广告 ===================================


    // =================================== 插屏广告 ===================================

    private final List<AbInsertAdWrapper> insertAdReadyList = new CopyOnWriteArrayList<>();
    private final List<AbInsertAdWrapper> insertAdLoadingList = new CopyOnWriteArrayList<>();
    private final Object insertAdLock = new Object();

    public boolean isInsertAdvertReady(@NonNull String placementId) {
        return insertAdReadyList.stream()
                .anyMatch(adWrapper -> Objects.equals(adWrapper.getPlacementId(), placementId) && adWrapper.isAdvertReady());
    }

    public boolean isInsertAdvertLoading(@NonNull String placementId) {
        return insertAdLoadingList.stream()
                .anyMatch(adWrapper -> Objects.equals(adWrapper.getPlacementId(), placementId) && adWrapper.isAdvertLoading());
    }

    @Nullable
    public AbInsertAdWrapper getReadyInsertAdWrapper(@NonNull String placementId) {
        return insertAdReadyList.stream()
                .filter(adWrapper -> Objects.equals(adWrapper.getPlacementId(), placementId) && adWrapper.isAdvertReady())
                .findFirst()
                .orElse(null);
    }

    public void updateInsertReadyList(@NonNull AbInsertAdWrapper adWrapper, boolean isRemove) {
        synchronized (insertAdLock) {
            if (isRemove) {
                insertAdReadyList.remove(adWrapper);
            } else if (!insertAdReadyList.contains(adWrapper)) {
                insertAdReadyList.add(adWrapper);
            }
        }
    }

    public void updateInsertLoadingList(@NonNull AbInsertAdWrapper adWrapper, boolean isRemove) {
        synchronized (insertAdLock) {
            if (isRemove) {
                insertAdLoadingList.remove(adWrapper);
            } else if (!insertAdLoadingList.contains(adWrapper)) {
                insertAdLoadingList.add(adWrapper);
            }
        }
    }

    // =================================== 插屏广告 ===================================


    // =================================== 激励广告 ===================================

    private final List<AbRewardAdWrapper> rewardAdReadyList = new CopyOnWriteArrayList<>();
    private final List<AbRewardAdWrapper> rewardAdLoadingList = new CopyOnWriteArrayList<>();
    private final Object rewardAdLock = new Object();

    public boolean isRewardAdvertReady(@NonNull String placementId) {
        return rewardAdReadyList.stream()
                .anyMatch(adWrapper -> Objects.equals(adWrapper.getPlacementId(), placementId) && adWrapper.isAdvertReady());
    }

    public boolean isRewardAdvertLoading(@NonNull String placementId) {
        return rewardAdLoadingList.stream()
                .anyMatch(adWrapper -> Objects.equals(adWrapper.getPlacementId(), placementId) && adWrapper.isAdvertLoading());
    }

    @Nullable
    public AbRewardAdWrapper getReadyRewardAdWrapper(@NonNull String placementId) {
        return rewardAdReadyList.stream()
                .filter(adWrapper -> Objects.equals(adWrapper.getPlacementId(), placementId) && adWrapper.isAdvertReady())
                .findFirst()
                .orElse(null);
    }

    public void updateRewardReadyList(@NonNull AbRewardAdWrapper adWrapper, boolean isRemove) {
        synchronized (rewardAdLock) {
            if (isRemove) {
                rewardAdReadyList.remove(adWrapper);
            } else if (!rewardAdReadyList.contains(adWrapper)) {
                rewardAdReadyList.add(adWrapper);
            }
        }
    }

    public void updateRewardLoadingList(@NonNull AbRewardAdWrapper adWrapper, boolean isRemove) {
        synchronized (rewardAdLock) {
            if (isRemove) {
                rewardAdLoadingList.remove(adWrapper);
            } else if (!rewardAdLoadingList.contains(adWrapper)) {
                rewardAdLoadingList.add(adWrapper);
            }
        }
    }

    // =================================== 激励广告 ===================================
}